﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using System.Data.SqlClient;
using TS_XY_Train_Repaire.UserCon.Main_Base;

namespace TS_XY_Train_Repaire.DocMana
{
    public partial class DocMana_Check_ManaFrom : DevExpress.XtraEditors.XtraForm
    {
        public DocMana_Check_ManaFrom()
        {
            InitializeComponent();
        }

        private void DocMana_Check_ManaFrom_Load(object sender, EventArgs e)
        {
            _LoadTreeList();
        }
        private void _LoadTreeList()
        {
            //tl_Level.Nodes.Clear();
            DataTable _dt = new DataTable();
            tl_Tree.Nodes.Clear();
            tl_Tree.Refresh();
            LeverString_SysTreeMainList _ls = new LeverString_SysTreeMainList();
            ArrayList pList = new ArrayList();
            //_ls = new LeverString_SysTreeMainList();
            //_ls.f_id = "M-1";
            //_ls.f_name = "系统底层数据目录";
            //_ls.f_imgindex = 4;
            //pList.Add(_ls);
            try
            {
                string strSQL = "select * from TS_S_T_P_Tree_main where f_level like '%,6,%' and f_del=0";
                //string strSQL = "select f_str_0 as f_string,* from TS_S_T_P_Tree where f_mainid=5 and f_type=1 and (f_id in (select f_menuid from TS_S_T_S_Tree_Authority where ((f_funcid=3 and f_type=0) or (f_funcid=5 and f_type=1) or ((f_funcid=14 or f_funcid in (select f_id from TS_S_T_P_Department where f_del=0 and f_level like '%,14,%')) and f_type=2)) and f_del=0) or f_func_0=0) and f_del=0";
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                SqlDataReader read = cmd.ExecuteReader();
                string _strUPID = "and (";
                int i = 0;
                while (read.Read())
                {
                    _ls = new LeverString_SysTreeMainList();
                    _ls.f_id = "T" + read["f_id"].ToString();
                    _ls.f_name = read["f_name"].ToString();
                    _ls.f_parent = "M-1";
                    _ls.f_deep = read["f_deep"].ToString();
                    _ls.f_level = read["f_level"].ToString();
                    _ls.f_type = read["f_type"].ToString();
                    _ls.f_str_0 = read["f_str_0"].ToString();
                    _ls.f_str_1 = read["f_str_1"].ToString();
                    _ls.f_str_2 = read["f_str_2"].ToString();
                    _ls.f_func_0 = read["f_func_0"].ToString();
                    _ls.f_func_1 = read["f_func_1"].ToString();
                    _ls.f_func_2 = read["f_func_2"].ToString();
                    if (read["f_type"].ToString() == "0")
                    {
                        _ls.f_imgindex = 5;
                    }
                    else
                    {
                        _ls.f_imgindex = 6;
                    }
                    if (i == 0)
                    {
                        _strUPID += "(f_level like '%," + read["f_id"].ToString() + ",%')";
                    }
                    else
                    {
                        _strUPID += "or (f_level like '%," + read["f_id"].ToString() + ",%')";
                    }
                    pList.Add(_ls);
                    i++;
                }
                //_strUPID += ")";
                //if (_strUPID.Length > 7)
                //{
                //    read.Close();
                //    strSQL = "select * from TS_S_T_P_Tree where  (f_id in (select f_menuid from TS_S_T_S_Tree_Authority where ((f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " and f_type=0) or (f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserPosID + " and f_type=1) or ((f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex + " or f_funcid in (select f_id from TS_S_T_P_Department where f_del=0 and f_level like '%," + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex + ",%')) and f_type=2)) and f_del=0) or f_func_0=0)   " + _strUPID + " and f_del=0 order by f_deep asc";
                //    //MessageBox.Show(strSQL);
                //    cmd = new SqlCommand(strSQL, conn);
                //    read = cmd.ExecuteReader();
                //    string _UPID = "";
                //    while (read.Read())
                //    {
                //        _ls = new LeverString_SysTreeMainList();
                //        _ls.f_id = "T" + read["f_id"].ToString();
                //        _ls.f_name = read["f_name"].ToString();
                //        _ls.f_deep = read["f_deep"].ToString();
                //        _ls.f_level = read["f_level"].ToString();
                //        _ls.f_type = read["f_type"].ToString();
                //        _ls.f_str_0 = read["f_str_0"].ToString();
                //        _ls.f_str_1 = read["f_str_1"].ToString();
                //        _ls.f_str_2 = read["f_str_2"].ToString();
                //        _ls.f_func_0 = read["f_func_0"].ToString();
                //        _ls.f_func_1 = read["f_func_1"].ToString();
                //        _ls.f_func_2 = read["f_func_2"].ToString();
                //        _UPID = read["f_level"].ToString();
                //        _UPID = _UPID.Substring(0, _UPID.Length - 1);
                //        _UPID = _UPID.Substring(_UPID.LastIndexOf(',') + 1);
                //        _ls.f_parent = "T" + _UPID;
                //        if (read["f_type"].ToString() == "0")
                //        {
                //            _ls.f_imgindex = 5;
                //        }
                //        else
                //        {
                //            _ls.f_imgindex = 6;
                //        }
                //        pList.Add(_ls);
                //    }
                //}
                read.Close();
                conn.Close();
                conn.Dispose();
            }
            catch
            {
            }
            tl_Tree.DataSource = pList;
        }

        private void tl_Tree_DoubleClick(object sender, EventArgs e)
        {
            string sql = "select * from TS_S_T_P_Tree where f_mainid=" + tl_Tree.FocusedNode["f_id"].ToString().Substring(1)+" f_del=0 and f_type=1";
            gridControl1.DataSource = TS_Win_Class.GetFilledDataTable(sql);
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (gridView1.GetFocusedRowCellValue("f_func_0").ToString()=="0")
                {
                    uc_Main_Base_CheckPDF bcp = new uc_Main_Base_CheckPDF();
                    uc_Main_Base_CheckPDF.fid = gridView1.GetFocusedRowCellValue("f_id").ToString();
                    bcp.ShowDialog();
                }
                else if (TS_Win_Class.GetFilledDataTable("select count(*) from TS_S_T_S_Tree_Authority where f_menuid in (select f_id from TS_S_T_P_Tree where f_level like '%,1616,%') and f_menuid=" + gridView1.GetFocusedRowCellValue("f_func_0").ToString() + " or (f_type=2 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex + ") or (f_type=1 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserPosID + ") or (f_type=0 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserCode + ")").Rows.Count > 0)
                {
                    uc_Main_Base_CheckPDF bcp = new uc_Main_Base_CheckPDF();
                    uc_Main_Base_CheckPDF.fid = gridView1.GetFocusedRowCellValue("f_id").ToString();
                    bcp.ShowDialog();
                }
                else {
                    MessageBox.Show("没有查看权限，请联系管理员！");
                }
            }
            catch (Exception)
            {
            }
        }

        private void btserch_Click(object sender, EventArgs e)
        {
            string sql = "select * from TS_S_T_P_Tree where f_mainid in (select f_id from TS_S_T_P_Tree_main where f_level like '%,6,%' and f_del=0) and f_type=1 and f_del=0 and f_name like '%"+tbname.Text.Trim()+"%'";
            gridControl1.DataSource = TS_Win_Class.GetFilledDataTable(sql);
        }
    }
}